/******************************************************************************************

Made by Flashphoner. All rights reserved.
These sources can only be used for working with Flashphoner server.
If you want use this to create your own application (separate from Flashphoner server)
please contact support@flashphoner.com 

******************************************************************************************/

package com.flashphoner.app;

import com.flashphoner.sdk.softphone.Logger;
import com.wowza.wms.server.IServerNotify;
import com.wowza.wms.server.IServer;
import com.wowza.wms.logging.WMSLogger;
import com.wowza.wms.logging.WMSLoggerFactory;
import com.flashphoner.sdk.rtmp.Config;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class FlashphonerServerListener implements IServerNotify {

    private String mode;

    private Process tcpdumpProcess;

    public void onServerCreate(IServer server) {

    }

    public void onServerInit(IServer server) {
        try {

            Config.getInstance();

            Logger.logger.info("Initializing flashphoner properties: " + Config.getInstance().getProperties());

            String date = new SimpleDateFormat("dd-MM-yyyy-HH-mm-ss").format(new Date());

            String wowzaHome = Config.WOWZA_HOME;

            String logName = wowzaHome + "/logs/tcpdump-" + date + ".pcap";

            mode = Config.getInstance().getProperty("mode");

            Logger.logger.info("Detecting start mode: " + mode + " ");

            if (mode != null) {
                Logger.logger.info("mode.length: " + mode.length());
            }

            if ("debug".equals(mode)) {

                Config.getInstance().setProperty("logs", Logger.DEBUG_MODE);

                Logger.init();

                try {
                    tcpdumpProcess = Runtime.getRuntime().exec("tcpdump udp -s 4096 -w " + logName);
                } catch (Exception e) {
                    Logger.logger.info("Tcpdump starting error. Check tcpdump command");
                }

                Logger.logger.info("Starting tcpdump process for: " + logName);

            }

            Logger.logger.info("Flashphoner build: "+Config.BUILD+" deployed.");

        } catch (Throwable e) {
            Logger.logger.error(e);
        }

    }

    public void onServerShutdownStart(IServer server) {
        try {
            if ("debug".equals(mode)) {
                if (tcpdumpProcess != null) {

                    try {
                        tcpdumpProcess.destroy();
                    } catch (Exception e) {
                        Logger.logger.info("Tcpdump process stoping error. Probably it is not exists.");
                    }
                    Logger.logger.info("Destroying tcpdump process...");
                }
            }
        } catch (Throwable e) {
            Logger.logger.error(e);
        }
    }

    public void onServerShutdownComplete(IServer server) {

    }

}
